class Pipe {
    constructor() {
        this.w = game.allImg['pipe_down'].width
        this.h = game.allImg['pipe_down'].height
        this.h1 = Math.round(Math.random() * 200 + 100);
        this.space = 140
        this.h2 = game.canvas.height - game.allImg["land"].height - this.h1 - this.space;
        this.x = game.canvas.width;
        this.speed = 1
        this.flag = true
        game.pipeArr.push(this)
    }

    update() {
        for (let i = 0; i < game.pipeArr.length; i++) {
            if (game.pipeArr[i].x < -this.w) {
                game.pipeArr.splice(i, 1)
            }
        }
        this.x -= this.speed

        this.x1 = this.x
        this.x2 = this.x + this.w
        this.y1 = this.h1
        this.y2 = this.h1 + this.space
        // 碰撞检测
        // 1. B.x2>= P.x1 && B.x1 <= P.x2 && B.y1 <= P.y1 && B.y2 >=  P.y2
        if (game.bird.x2 >= this.x1 && game.bird.x1 <= this.x2 && (game.bird.y1 <= this.y1 || game.bird.y2 >= this.y2)) {
            game.sceneManager.enter(3)
        }
        if(this.flag && game.bird.x1>this.x2) {
            game.score++
            this.flag = false
        }
    }
    render() {
        game.draw.drawImage(game.allImg["pipe_down"], 0, this.h - this.h1, this.w, this.h1, this.x, 0, this.w, this.h1);
        game.draw.drawImage(game.allImg["pipe_up"], 0, 10, this.w, this.h2, this.x, this.h1 + this.space, this.w, this.h2);
    }
}